<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include( '../global/initialize.php' );

$query = "SELECT faction_id, access, countdown, leader_vote " .
		"FROM faction_member " .
		"WHERE ruler_id = " . $_SESSION['ruler_id'] . " AND requested = 0 AND invited = 0";
$result = mysql_query($query) or die("Query failed : " . mysql_error());
$row = mysql_fetch_array($result, MYSQL_ASSOC);
if (isset ($row['faction_id']) && $row['countdown'] == 0) {
	aos_header( 'Diplomacy' );
	
	$access = $row['access'];
	$faction_id = $row['faction_id'];
	$leader_vote = $row[ 'leader_vote' ];
?>
					<div class="blok">
<?php
	printDiplomacyTab('');
?> 
					</div>
					<br/>
<?php
	$query = "SELECT control_type, leader, faction_type_id " .
				"FROM faction " .
				"WHERE faction_id = $faction_id";
	$result = mysql_query($query) or die("Query failed : " . mysql_error());
	$row = mysql_fetch_array($result, MYSQL_ASSOC);
	$control_type = $row['control_type'];
	$leader = $row['leader'];
	$faction_type_id = $row[ 'faction_type_id' ];

	$query = "SELECT turn, turns_inactive " .
				"FROM game";
	$result = mysql_query($query) or die("Query failed : " . mysql_error());
	$row = mysql_fetch_array($result, MYSQL_ASSOC);
	$turns_to_inactive = $row['turns_inactive'];
	$current_turn = $row['turn'];
	
	$query = "SELECT count(*) " .
			"FROM faction_member " .
			"WHERE requested = 0 AND invited = 0 AND faction_id = " . $faction_id;
	$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$row = mysql_fetch_array( $result, MYSQL_ASSOC );
	$members = $row[ 'count(*)' ];

	if( isset( $_SESSION[ 'faction_remove_status' ] ) ) {
		print( '<div class="blok">' . translate( 'Status' ) . ": " . translate( $_SESSION[ 'faction_remove_status' ] ) . "</div><br />" );
		unset( $_SESSION[ 'faction_remove_status' ] );
	}	
?>
					<div class="blok">
						<strong>Actions</strong><br />
<?php
	$something = false;
	if( $_SESSION[ 'ruler_id' ] != $leader ) {
		$something = true;
?>
							<form method="post" action="action/kick_member.php">
								<input type="hidden" name="ruler_id" value="<?php print( $_SESSION[ 'ruler_id' ] ); ?>" />
								<input type="checkbox" name="check_remove" /> <?php transprint( 'I want to leave this faction' ); ?><br />
								<input type="submit" value="<?php transprint( 'Confirm' ); ?>" /><br />
							</form>
<?php
	}
	else {
		if( $row[ 'count(*)' ] == 1 ) {
			$something = true;
?>			
							<form method="post" action="action/disband.php">
								<input type="submit" value="<?php transprint( 'disband faction' ); ?>" />
							</form>
<?php
		}
	}
	
	if( $access >= 10 ) {
		if( $something ) {
			$something = false;
			print( '<br />' );
		}
		$query = "SELECT * " .
				"FROM faction_types " .
				"WHERE max_members >= " . $members . " " .
				"ORDER by upkeep, max_members";
		$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
		$num = mysql_numrows( $result );
		if( $num > 1 ) {
?>
							<form method="post" action="action/change_type.php">
								<?php transprint( 'Faction type' ); ?>: <select name="faction_type_id" onchange="this.parentNode.submit();">
<?php			
			$i = 0;
			while ( $i < $num ) {
				if( $faction_type_id == mysql_result( $result, $i, "faction_type_id" ) ) {
					$selected = " selected=\"selected\"";
				}
				else {
					$selected = "";
				}
?>
									<option<?php print( $selected ); ?> value=<?php print( mysql_result( $result, $i, "faction_type_id" ) ); ?>"><?php transprint( mysql_result( $result, $i, "type_name" ) ); ?></option>
<?php			    
				$i ++;
			}
?>		
								</select>
							</form>
<?php
		}
		else {
			$query = "SELECT type_name " .
					"FROM faction_types " .
					"WHERE faction_type_id = " . $faction_type_id;
			$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
			$row = mysql_fetch_array( $result, MYSQL_ASSOC );
?>
							<?php transprint( 'Faction type' ); ?>: <?php transprint( $row[ 'type_name' ] ); ?>
<?php			
		}
?>
							<br />
							<form method="post" action="action/change_control_type.php">
								<?php transprint( 'Faction structure' ); ?>: <select name="control_type" onchange="this.parentNode.submit();">
									<option<?php if( $control_type == 0 ) print( ' selected="selected"' ); ?> value="Hierarchic"><?php transprint( 'Hierarchic' ); ?></option>
									<option<?php if( $control_type == 1 ) print( ' selected="selected"' ); ?> value="Democratic"><?php transprint( 'Democratic' ); ?></option>
								</select>
							</form>
							<br />
							<a href="faction_settings.php"><?php transprint( 'Faction settings' ); ?></a><br />
							<a href="manage_faction_channels.php"><?php transprint( 'Manage faction chat channels' ); ?></a><br />
<?php	
	}
	else {
			if( $control_type == 0 ) {
				$structure = translate( 'Hierarchic' );
			}
			else {
				$structure = translate( 'Democratic' );
			}
			$query = "SELECT type_name " .
					"FROM faction_types " .
					"WHERE faction_type_id = " . $faction_type_id;
			$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
			$row = mysql_fetch_array( $result, MYSQL_ASSOC );
?>
							<?php transprint( 'Faction type' ); ?>: <?php transprint( $row[ 'type_name' ] ); ?><br />
							<?php transprint( 'Faction structure' ); ?>: <?php print( $structure ); ?><br />
<?php	
	}
	if( $access >= 5 ) {
		$query = "SELECT count(*) " .
				"FROM faction_member " .
				"WHERE faction_id = " . $faction_id . " AND requested = 1 AND invited = 0";
		$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
		$row = mysql_fetch_array( $result, MYSQL_ASSOC );
		if( $row[ 'count(*)' ] > 0 ) {
			if( $row[ 'count(*)' ] == 1 ) {
				$join_reqs = "(" . $row[ 'count(*)' ] . " " . translate( 'ruler has requested to join the faction' ) . ")";
			}
			else {
				$join_reqs = "(" . $row[ 'count(*)' ] . " " . translate( 'rulers have requested to join the faction' ) . ")";
			}	
		}
		else {
			$join_reqs = "";	
		}
		print( "<a href=\"invite_list.php\">" . translate( 'Invite/accept new members' ) . "</a> $join_reqs<br />\r\n" );
	}
	
	
	if( $control_type == 1 ) {
		$election_day = 96 - ( $current_turn % 96 - 6 );
		if( $election_day > 95 ) {
			$election_day -= 96;
		}
		
		if( $election_day == 1 ) {
			$election_day = '1 ' . translate( 'day' );
		}
		else {
			$election_day = $election_day . ' ' . translate( 'days' );
		}
?>
						<br />
						<?php transprint( 'Next leader election is in' ); ?> <?php print( $election_day ); ?>.<br />
<?php
	}
?>
						</div>
						<br />
<?php
	$query = "SELECT * " .
			"FROM faction_member " .
			"JOIN ruler ON ruler.ruler_id = faction_member.ruler_id " .
			"WHERE faction_id = $faction_id AND requested = 0 AND invited = 0 " .
			"ORDER BY score DESC";
	$result = mysql_query($query) or die("Query failed : " . mysql_error());
	$num = mysql_numrows($result);
	$i = 0;
?>
					<table>
						<th colspan="<?php 
						$count = 6;
						if( $control_type == 1) {
							$count ++;
						}
						if( $access >= 5 ) {
							$count ++;
						}
						print( $count );
						?>"><?php transprint( 'Member details' ); ?></th>
						<tr>
							<td><?php transprint( 'Title' ); ?></td>
							<td><?php transprint( 'Name' ); ?></td>
							<td align="right"><?php transprint( 'Tax income' ); ?></td>
							<td align="right"><?php transprint( 'Iron income' ); ?></td>
							<td align="right"><?php transprint( 'Coal income' ); ?></td>
							<td><?php transprint( 'Function' ); ?></td>
<?php 
	if( $control_type == 1 ) {
?>
							<td><?php transprint( 'Vote' ); ?>
<?php
	}
	if( $access >= 5 ) {
?>
							<td><?php transprint( 'Remove' ); ?>
<?php
	}
?>
						</tr>
<?php
	while ($i < $num) {
		if ($i % 2 == 0) {
			print "<tr class=\"even\">";
		} else {
			print "<tr>";
		}
		$ruler_name = mysql_result($result, $i, "name");

		if ( $access >= 1 ) {
			print ("<td><form method=\"post\" action=\"action/change_title.php\" ><input type=\"hidden\" name=\"ruler_id\" value=\"" . mysql_result($result, $i, "ruler_id") . "\" /><input type=\"text\" name=\"title\" size=\"16\" value=\"" . stripslashes(mysql_result($result, $i, "title")) . "\" /> <input type=\"submit\" value=\"" . translate('set') . "\" /></form></td>");
		} else {
			print ("<td>" . stripslashes(mysql_result($result, $i, "title")) . "</td>");
		}
		$member_time = number_format(mysql_result($result, $i, "member_time"));
		if (mysql_result($result, $i, "member_time") == 1) {
			$member_time = $member_time . " " . translate('day');
		} else {
			$member_time = $member_time . " " . translate('days');
		}

		$query2 = "SELECT banned " .
			"FROM users " .
			"WHERE user_id = " . mysql_result($result, $i, "user_id");
		$result2 = mysql_query($query2) or die("Query failed : " . mysql_error());
		$row2 = mysql_fetch_array($result2, MYSQL_ASSOC);
		if (mysql_result($result, $i, "ban_turn") > $current_turn || (isset ($row2["banned"]) && $row2["banned"] == 1)) {
			$inactive = " (" . translate('banned') . ")";
		}
		elseif (mysql_result($result, $i, "resigned") == 1) {
			$inactive = " (" . translate('resigned') . ")";
		}
		elseif (mysql_result($result, $i, "active_turn") + $turns_to_inactive < $current_turn) {
			$inactive = " (" . translate('inactive') . ")";
		} else {
			$inactive = "";
		}

		if ($current_turn == mysql_result($result, $i, "active_turn")) {
			$online = "<strong>";
			$online_after = "</strong>";
		} else {
			$online = "";
			$online_after = "";
		}

		print ("<td><a href=\"../overview/ruler.php?name=" . $ruler_name . "\">$online$ruler_name$online_after</a>$inactive</td>");
		print ("<td align=\"right\"><strong class=\"gold\">" . number_format( mysql_result($result, $i, 'lastday_taxes') ) . "</strong></td>");
		print ("<td align=\"right\"><strong class=\"iron\">" . number_format( mysql_result($result, $i, 'iron_income') ) . "</strong></td>");
		print ("<td align=\"right\"><strong class=\"coal\">" . number_format( mysql_result($result, $i, 'coal_income') ) . "</strong></td>");
		
		if( $access >= 10 && mysql_result($result, $i, 'ruler_id') != $leader ) {
?>
							<td>
								<form action="action/change_level.php" method="post">
									<input type="hidden" name="ruler_id" value="<?php print( mysql_result($result, $i, 'ruler_id') ); ?>" />
									<select name="access" onchange="this.parentNode.submit();">
										<option value="0"<?php if( mysql_result($result, $i, 'access') == 0 ) print( ' selected="selected"' ); ?>><?php transprint( 'Member' ); ?></option>
										<option value="1"<?php if( mysql_result($result, $i, 'access') == 1 ) print( ' selected="selected"' ); ?>><?php transprint( 'Assistant' ); ?></option>
										<option value="5"<?php if( mysql_result($result, $i, 'access') == 5 ) print( ' selected="selected"' ); ?>><?php transprint( 'Organizer' ); ?></option>
										<option value="10"<?php if( mysql_result($result, $i, 'access') == 10 ) print( ' selected="selected"' ); ?>><?php transprint( 'Vice Leader' ); ?></option>
									</select>
								</form>
							</td>
<?php
		}
		else {
			print ("<td>" . get_access_string( mysql_result($result, $i, 'access'), ( $leader == mysql_result($result, $i, 'ruler_id') ) ) . "</td>");
		}
		
		if( $control_type == 1 ) {
			$query2 = "SELECT count(*) " .
				"FROM faction_member " .
				"WHERE leader_vote = " . mysql_result($result, $i, "ruler_id") . " AND faction_id = $faction_id AND invited = 0 AND requested = 0";
			$result2 = mysql_query($query2) or die("Query failed : " . mysql_error());
			$row2 = mysql_fetch_array($result2, MYSQL_ASSOC);
			$votes = number_format( $row2[ 'count(*)' ] );
			if( $leader_vote == mysql_result($result, $i, 'ruler_id') ) {
?>
							<td>
								<form method="post" action="action/vote_for.php">
									<input type="hidden" name="ruler_id" value="-1" />
									<?php print( $votes ); ?> - <input type="submit" value="<?php transprint( 'Withdraw vote' ); ?>" />
								</form>
							</td>
<?php
			}
			else {
?>
							<td>
								<form method="post" action="action/vote_for.php">
									<input type="hidden" name="ruler_id" value="<?php print( mysql_result($result, $i, 'ruler_id') ); ?>" />
									<?php print( $votes ); ?> - <input type="submit" value="<?php transprint( 'Vote' ); ?>" />
								</form>
							</td>
<?php
			}
		}
		
		if( $access >= 5 && mysql_result($result, $i, 'ruler_id') != $leader 
				&& mysql_result($result, $i, 'ruler_id') != $_SESSION[ 'ruler_id' ] 
				&& ( $access > mysql_result($result, $i, 'access') || $access >= 10 ) ) {
?>
							<td>
								<form method="post" action="action/kick_member.php">
									<input type="hidden" name="ruler_id" value="<?php print( mysql_result($result, $i, 'ruler_id') ); ?>" />
									<input type="checkbox" name="check_remove" /> <?php transprint( 'Kick' ); ?> <input type="submit" value="<?php transprint( 'Confirm' ); ?>" />
								</form>
							</td>
<?php
		}
		elseif( $access >= 5 ) {
?>
							<td>
								&nbsp;
							</td>
<?php
		}
		print ("</tr>\r\n");
		$i++;
	}
	print ("</table>\r\n");
	aos_footer();
} 
else {
	header("Location: diplomacy.php");
}
?>